﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class Student
{
    public string FirstName { get { return this.firstName; } }
    public string LastName { get { return this.lastName; } }
    public int Fn { get { return this.fn; } }
    public string Tel { get { return this.tel; } }
    public string Email { get { return this.email; } }
    public List<int> Marks { get { return this.marks; } }
    public int GroupNumber { get { return this.groupNumber; } }

    private string firstName;
    private string lastName;
    private int fn;
    private string tel;
    private string email;
    private List<int> marks;
    private int groupNumber;


    public Student() :
        this(null, null, 0, null, null, null, 0)
    {}

    public Student (string firstName, string lastName, int fn, string tel, string email, List<int> marks, int groupNumber)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.fn = fn;
        this.tel = tel;
        this.email = email;
        this.marks = marks;
        this.groupNumber = groupNumber;
    }

    public override string ToString()
    {
        return FirstName + " " + LastName+", " + Fn + ", tel: " + Tel + ", email: "+Email + ", groupNm: "+ groupNumber;
    }
}

class StudentFullName
{
    public string FullName { get; set; }

    public StudentFullName()
        : this("")
    {
    }

    public StudentFullName(string Name)
    {
        this.FullName = Name;
    }
}

class LINQ_Student
{
    public static void SelectGroupLinq(List<Student> listOfStudents, int groupNum)
    {
        Console.WriteLine("Students in group: {0} ", groupNum);

        IEnumerable<Student> subset = from student in listOfStudents
                                      where student.GroupNumber == groupNum
                                      orderby student.LastName
                                      select student;

        int count = 0;
        foreach (Student student in subset)
        {
            count++;
            Console.WriteLine(student);
        }
        if (count == 0)
            Console.WriteLine("No students in group {0}.", groupNum);
    }

    public static void SelectGroupExtensionMethods(List<Student> listOfStudents, int groupNum)
    {
        Console.WriteLine("Students in group: {0} ", groupNum);

        var subset = listOfStudents.Where(student => student.GroupNumber == groupNum).OrderBy(student => student.LastName);

        int count = 0;
        foreach (Student student in subset)
        {
            count++;
            Console.WriteLine(student);
        }
        if (count == 0)
            Console.WriteLine("No students in group {0}.", groupNum);
    }

    public static void SelectEmail(List<Student> listOfStudents, string substring)
    {
        Console.WriteLine("Students with: {0} in their email",substring);

        //var subset = listOfStudents.Where(student => student.Email.Contains(substring)).OrderBy(student => student.LastName);

        IEnumerable<Student> subset = from student in listOfStudents
                                      where student.Email.Contains(substring)
                                      orderby student.LastName
                                      select student;

        int count = 0;
        foreach (Student student in subset)
        {
            count++;
            Console.WriteLine(student);
        }
        if (count == 0)
            Console.WriteLine("None.");
    }

    public static void SelectWithMark(List<Student> listOfStudents, int mark)
    {
        //List<StudentFullName> excellentStudents = new List<StudentFullName>();
        //var subset = listOfStudents.Where(s => s.Marks.Contains(mark));
        //foreach (var s in subset)
        //{
        //    excellentStudents.Add(new StudentFullName(s.FirstName+" "+s.LastName));
        //    Console.WriteLine(s.LastName);
        //}

        var studentsWith6 =
            from student in listOfStudents
            where student.Marks.Contains(mark)
            select new { FullName = student.FirstName + " " + student.LastName };

        foreach (var s in studentsWith6)
        {
            Console.WriteLine(s.FullName);
        }

    }

    public static void SelectByMarkTimes(List<Student> listOfStudents, int times, int mark)
    {
        var studentsWith22 =
            from student in listOfStudents
            where GetMarkCount(student, mark) == times
            select new { Fullname = student.FirstName + " " + student.LastName };

        foreach (var s in studentsWith22)
        {
            Console.WriteLine(s.Fullname);
        }
    }

    public static int GetMarkCount(Student student, int target)
    {
        int count = 0;

        foreach (int mark in student.Marks)
        {
            if (mark == target)
            {
                count++;
            }
        }

        return count;
    }

    static void Main(string[] args)
    {
        List<Student> listOfStudents = new List<Student>();

        listOfStudents.Add(new Student("Hristo", "Georgiev", 61325, "0888333444", "bateto_pro@abv.bg", new List<int> { 6, 4, 6, 5, 5, 6 }, 3));
        listOfStudents.Add(new Student("Kiro", "Pacanov", 23456, "0898356734", "kircho@yahoo.com", new List<int> { 6, 4, 6, 5, 5, 6 }, 2));
        listOfStudents.Add(new Student("Rumbata", "Ton4ev", 45654, "0883374684", "dogy@abv.bg", new List<int> { 2, 4, 3, 5, 5, 5 }, 1));
        listOfStudents.Add(new Student("Miro", "Ionkov", 34565, "0878756878", "miroto@gmail.com", new List<int> { 6, 4, 6, 5, 5, 6 }, 2));
        listOfStudents.Add(new Student("Aleksanadar", "Engiozov", 45689, "0876345474", "engiozi@abv.bg", new List<int> { 4, 4, 2, 3, 5, 2 }, 4));
        listOfStudents.Add(new Student("Vasil", "Karaionchev", 54677, "0889583496", "karaionata@abv.bg", new List<int> { 6, 6, 5, 6, 5, 6 }, 3));

        //SelectGroupLinq(listOfStudents,3);
        //SelectGroupExtensionMethods(listOfStudents, 3);
        //SelectEmail(listOfStudents, "abv");
        SelectWithMark(listOfStudents, 6);
        //SelectByMarkTimes(listOfStudents, 3, 6);
    }
}